<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Programs_Model extends CI_Model{
	protected $_name = "programs";
	protected $_primary = "ProgramId";
	var $NameProgram_vn = "NameProgram_vn", $NameProgram_en = "NameProgram_en", $Date = "Date", $Time = "Time", $Description_vn = "Description_vn", $Description_en = "Description_en", $ChannelId = "ChannelId", $Status = "Status";
	
	function getProgramsByDateAndChannelId($ChannelId, $Date){
		$d = explode('-', $Date);
    	$Date = date('Y-m-d', mktime(0,0,0,$d[1],$d[0],$d[2]));
		$this->db->select($this->_name.".*, channel.NameChannel_vn, channel.Logo");
		$this->db->join("channel","channel.ChannelId=".$this->_name.".ChannelId");
		$this->db->where("DATE(".$this->Date.")", $Date);
		$this->db->where($this->_name.".".$this->ChannelId, $ChannelId);
		$where = "(".$this->_name.".".$this->Status."=1 OR ".$this->_name.".".$this->Status."=2)";
		$this->db->where($where);
		$this->db->order_by($this->Time, 'ASC');
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function getMinDateOfPrograms(){
		$this->db->select('Date');
		$this->db->order_by($this->Date, 'ASC');
		$this->db->limit(1);
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$strtotime = strtotime($query->row()->Date);
	        $date = date('d-m-Y',$strtotime);
			return $date;
		}
		return false;
	}
	
	function searchPrograms($keysearch){
		$this->db->select($this->_name.".*, channel.NameChannel_vn, channel.Logo");
		$this->db->join("channel","channel.ChannelId=".$this->_name.".ChannelId");
		$this->db->like($this->NameProgram_vn, $keysearch);	
		$where = "(".$this->_name.".".$this->Status."=1 OR ".$this->_name.".".$this->Status."=2)";
		$this->db->where($where);
		$this->db->order_by($this->Date, 'DESC');
		$query = $this->db->get($this->_name);
		if($query->num_rows() > 0){
			$i = 0;
			$j = 0;
			foreach($query->result() as $row){
				$data[$j] = $row;
				$i++;
				$data[$j]->stt = $i;
				$j++;
			}
			return $data;
		}
		return false;
	}
	
	function changeStatusProgram($ProgramId, $Status){
		try{
			$this->db->where($this->_primary, $ProgramId);
			if($Status == 1){
				$Status = 2;
			}elseif($Status == 2){
				$Status = 1;
			}
			$this->db->update($this->_name, array($this->Status => $Status));
			return $Status;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function deleteProgram($ProgramId){
		try{
			$this->db->where($this->_primary, $ProgramId);
			$this->db->delete($this->_name);
			return true;
		}catch(Exception $ex){
			return false;
		}
	}
	
	function insertProgram(){
		$this->db->set($this->NameProgram_vn, trim($this->input->post($this->NameProgram_vn, true)));
		$this->db->set($this->NameProgram_en, trim($this->input->post($this->NameProgram_en, true)));
		$this->db->set($this->Description_vn, trim($this->input->post($this->Description_vn)));
		$this->db->set($this->Description_en, trim($this->input->post($this->Description_en)));
		$d = explode('-', $this->input->post($this->Date, true));
		$Date = date('Y-m-d', mktime(0,0,0,$d[1],$d[0],$d[2]));
		$this->db->set($this->Date, $Date);
		$this->db->set($this->Time, trim($this->input->post($this->Time, true)));
		$this->db->set($this->ChannelId, trim($this->input->post($this->ChannelId, true)));
		$this->db->set($this->Status, 1);
		$this->db->insert($this->_name);
	}
	
	function updateProgram($ProgramId){
		$d = explode('-', $this->input->post($this->Date, true));
		$Date = date('Y-m-d', mktime(0,0,0,$d[1],$d[0],$d[2]));
		$data = array(
					$this->NameProgram_vn => trim($this->input->post($this->NameProgram_vn, true)),
					$this->NameProgram_en => trim($this->input->post($this->NameProgram_en, true)),
					$this->Description_vn => trim($this->input->post($this->Description_vn)),
					$this->Description_en => trim($this->input->post($this->Description_en)),
					$this->Date => $Date,
					$this->Time => trim($this->input->post($this->Time, true)),
					$this->ChannelId => trim($this->input->post($this->ChannelId, true))
				);
		$this->db->where($this->_primary, $ProgramId);
		$this->db->update($this->_name, $data);
	}
	
	function getProgramByProgramId($ProgramId){
		$this->db->where($this->_primary, $ProgramId);
		$where = "(".$this->Status."=1 OR ".$this->Status."=2)";
		$this->db->where($where);
		$query = $this->db->get($this->_name);
		return $query->row();
	}
}